package Visitor;

import Entities.*;
import Entities.Advantages.DecreaseBallSpeed;
import Entities.Advantages.DecreaseBarSize;
import Entities.Advantages.IAdvantage;
import Entities.Advantages.IncreaseBallSpeed;
import Entities.Advantages.IncreaseBarSize;

public class MoveVisitor implements Visitor.IVisitor {
    private boolean leftPressed;
    private boolean rightPressed;
    private boolean spacePressed;
    private int maxWidth;
    private final int pixels = 3;

    public MoveVisitor(boolean leftPressed, boolean rightPressed, boolean spacePressed, int maxWidth) {
        this.leftPressed = leftPressed;
        this.rightPressed = rightPressed;
        this.spacePressed = spacePressed;
        this.maxWidth = maxWidth;
    }

    @Override
    public Drawable visit(Bar bar) {
        if (leftPressed && !rightPressed){
            if (bar.getX() - pixels <= 0){
                bar.setX(1);
            }
            else {
                bar.setX(bar.getX() - pixels);
            }
        }
        else if (!leftPressed && rightPressed){
            if (bar.getX() + bar.getWidth() + pixels > maxWidth - 1){
                bar.setX(maxWidth - bar.getWidth() - 2);
            }
            else {
                bar.setX(bar.getX() + pixels);
            }
        }
        return null;
    }

    @Override
    public Drawable visit(Ball ball) {
        if (spacePressed && ball.getDx() == 0 && ball.getDy() == 0){
            ball.setDx(1);
            ball.setDy(-1);
        }
        else if (!spacePressed && ball.getDx() == 0 && ball.getDy() == 0 && leftPressed && !rightPressed){
            if (ball.getX() - pixels < 6){
                ball.setX(6);
            }
            else ball.setX(ball.getX() - pixels);
        }
        else if (!spacePressed && ball.getDx() == 0 && ball.getDy() == 0 && !leftPressed && rightPressed){
            if (ball.getX() + ball.getRadio() * 2 + pixels > maxWidth - 6){
                ball.setX(maxWidth - ball.getRadio() * 2 - 6);
            }
            else{ 
                ball.setX(ball.getX() + pixels);
            }
        }
        else{
            ball.setX(ball.getX() + ball.getDx());
            ball.setY(ball.getY() + ball.getDy());
        }
        return null;
    }

    @Override
    public Drawable visit(Boundary boundary) {
        return null;
    }

    @Override
    public Drawable visit(IAdvantage advantage) {
        advantage.setY(advantage.getY() + advantage.getDy());
        return null;
    }

    @Override
    public Drawable visit(Brick brick) {
        return null;
    }

    @Override
    public Drawable visit(Drawable drawable) {
        return null;
    }

    @Override
    public Drawable visit(Hinder hinder) {
        return null;
    }

    @Override
    public Drawable visit(DecreaseBarSize dicreaseBarSize) {
        dicreaseBarSize.setY(dicreaseBarSize.getY() + dicreaseBarSize.getDy());
        return null;
    }

    @Override
    public Drawable visit(IncreaseBarSize increaseBarSize) {
        increaseBarSize.setY(increaseBarSize.getY() + increaseBarSize.getDy());
        return null;
    }

    @Override
    public Drawable visit(DecreaseBallSpeed drecreaseBallSpeed) {
        drecreaseBallSpeed.setY(drecreaseBallSpeed.getY() + drecreaseBallSpeed.getDy());
        return null;
    }

    @Override
    public Drawable visit(IncreaseBallSpeed increaseBallSpeed) {
        increaseBallSpeed.setY(increaseBallSpeed.getY() + increaseBallSpeed.getDy());
        return null;
    }
}
